package main

import (
    "log"
    "net/http"
    "time"
    "github.com/googollee/go-socket.io"
)

func main() {
    server, err := socketio.NewServer(nil)
    if err != nil {
        log.Fatal(err)
    }
    server.On("connection", func(so socketio.Socket) {
        log.Println("on connection")

        so.Emit("push", `{ hello1: "world" }`);
        so.On("push_ack", func(data string) {
          log.Println("received push_ack: ", time.Now(), data);
        });

        timer := time.NewTicker(2*time.Second)

        select {
            case <-timer.C:
                so.Emit("push", "{time1:" + time.Now().Format("2006-01-02 15:04:05") + "}");

        }

        so.On("disconnection", func() {
            log.Println("on disconnect")
        })
    })
    server.On("error", func(so socketio.Socket, err error) {
        log.Println("error:", err)
    })

    http.Handle("/api/ws/dataset/", server)
    http.Handle("/", http.FileServer(http.Dir("../ns1.0/hotdocs")))
    log.Println("Serving at localhost:10000...")
    log.Fatal(http.ListenAndServe(":10000", nil))
}